package ca.etsmtl.log121.tp3.model;

import java.awt.Image;

public class ImageModel {
	public static final int THUMBNAIL_WIDTH = 200;
	public static final int THUMBNAIL_HEIGHT = 200;

	private Image thumbnail;
	private Image image;

	public ImageModel(Image image){
		this.image = image;

		buildThumbnailFromImage(image);
	}

	private void buildThumbnailFromImage(Image image) {
		thumbnail = null;

		int height = image.getHeight(null);
		int width = image.getWidth(null);

		if (height == width) {
			thumbnail = image.getScaledInstance(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, Image.SCALE_SMOOTH);
		} else if (height > width) {
			int calculatedWidth = (THUMBNAIL_HEIGHT * width) / height;

			thumbnail = image.getScaledInstance(calculatedWidth, THUMBNAIL_HEIGHT, Image.SCALE_SMOOTH);
		} else { // (height < width)
			int calculatedHeight = (THUMBNAIL_WIDTH * height) / width;

			thumbnail = image.getScaledInstance(THUMBNAIL_WIDTH, calculatedHeight, Image.SCALE_SMOOTH);
		}
	}

	public Image getThumbnail() {
		return thumbnail;
	}

	public Image getImage() {
		return image;
	}

}
